﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace winformLesson1
{
    public partial class Main : Form
    {
        private int userId;
        private User user;

        public Main()
        {
            InitializeComponent();
        }
        public Main(int userId)
        {
            InitializeComponent();
            this.userId = userId;
            GetUserInfo(userId);
        }


        /// <summary>
        /// 获取用户信息
        /// </summary>
        /// <param name="userId">用户ID</param>
        private void GetUserInfo(int userId)
        {
            string sql = string.Format(@"SELECT * FROM [User] where UserId=@UserId");
            SqlParameter[] paras = new SqlParameter[]
            {
                new SqlParameter("@UserId",userId)
            };
            user = SqlHelper.GetModel<User>(sql, paras);
            if (user != null)
            {
                lbl_UserName.Text = "欢迎你：" + user.UserName;
                lbl_UserId.Text = "你是第 " + user.UserId + " 位注册用户";
                lbl_regTime.Text = "你于 " + user.CreateTime.ToString("yyyy年MM月dd日HH时mm分ss秒") + " 注册";
                tbx_Desc.Text = user.Desc;
            }
            

            //using (SqlConnection conn = new SqlConnection())
            //using (SqlCommand comm = new SqlCommand(sql, conn))
            //using (IDataReader reader = SqlHelper.ExecuteReader(conn, comm, sql, paras))
            //{
            //    if (reader.Read())
            //    {
            //        lbl_UserName.Text = "欢迎你：" + reader["UserName"];  //reader.GetString(1);
            //        lbl_UserId.Text = "你是第 " + reader.GetInt32(0) + " 位注册用户";
            //        lbl_regTime.Text = "你于 " + reader.GetDateTime(4).ToString("yyyy年MM月dd日HH时mm分ss秒") + " 注册";
            //        tbx_Desc.Text = reader.GetString(3);
            //    }
            //}

        }

        private void Main_Load(object sender, EventArgs e)
        {

        }

        private void Main_FormClosed(object sender, FormClosedEventArgs e)
        {
            
        }

        private void Main_FormClosing(object sender, CancelEventArgs e)
        {
            this.OnClosing(e);
            Application.Exit(e);
        }

        private void button1_Click(object sender, EventArgs e)
        {

        }
    }
}
